<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" border :height="autoHeight">
      <el-table-column prop="leftTable" label="左表" align="center">
        <template slot-scope="scope">
          <el-select style="width: 100%" v-model="scope.row[scope.column.property]">
            <el-option v-for="item in modelInfo.tables" :key="item.jdbcName" :value="item.jdbcName" :label="item.jdbcName"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="leftColumn" label="左列" align="center">
        <template slot-scope="scope">
          <el-select style="width: 100%" v-model="scope.row[scope.column.property]">
            <template v-for="item in modelInfo.fields">
              <el-option v-if="item.jdbcTable === scope.row.leftTable" :key="item.jdbcName" :value="item.jdbcName" :label="item.jdbcName"></el-option>
            </template>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="joinType" label="连接类型" align="center">
        <template slot-scope="scope">
          <el-select style="width: 100%" v-model="scope.row[scope.column.property]">
            <el-option v-for="item in joinTypes" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="rightTable" label="右表" align="center">
        <template slot-scope="scope">
          <el-select style="width: 100%" v-model="scope.row[scope.column.property]">
            <el-option v-for="item in modelInfo.tables" :key="item.jdbcName" :value="item.jdbcName" :label="item.jdbcName"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="rightColumn" label="右列" align="center">
        <template slot-scope="scope">
          <el-select style="width: 100%" v-model="scope.row[scope.column.property]">
            <template v-for="item in modelInfo.fields">
              <el-option v-if="item.jdbcTable === scope.row.rightTable" :key="item.jdbcName" :value="item.jdbcName" :label="item.jdbcName"></el-option>
            </template>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="joinOrder" label="顺序" align="center" width="100px">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row[scope.column.property]" :placeholder="scope.column.label" controls-position="right" clearable style="width: 100%"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120px">
        <template slot="header" slot-scope="scope">
          <el-button v-if="scope" type="primary" @click="addJoin" icon="el-icon-circle-plus-outline">新增</el-button>
        </template>
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle @click="removeJoin(scope.row, scope.$index)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "ModelJoin",
  mixins: [IdeaTable],
  inject: {
    modelInfo: {
      type: 'modelInfo',
      default: function () {
        return []
      }
    }
  },
  computed: {
    autoHeight(){
      return `calc(100vh - 375px)`
    }
  },
  data() {
    return {
      joinTypes: [
        { value: 'left join', label: 'left join' },
        { value: 'right join', label: 'right join' },
        { value: 'inner join', label: 'inner join' }
      ]
    }
  },
  watch: {
    'modelInfo.joins'(){
      this.initSource()
    }
  },
  created() {
    this.rowKey = 'joinId'
    this.pageInfo.pageSize = 10
    if (!this.modelInfo.joins) {
      this.$set(this.modelInfo, 'joins', [])
    }
    this.initSource()
  },
  methods: {
    initSource() {
      this.dataSource.splice(0, this.dataSource.length)
      this.pageInfo.total = 0
      const { joins } = this.modelInfo
      if (joins && joins.length > 0) {
        this.pageInfo.total = joins.length
        joins.forEach(item => this.dataSource.push(item))
      }
    },
    addJoin(){
      const join = { joinOp: '=', editAbled: true, joinOrder: this.modelInfo.joins.length }
      this.dataSource.splice(0, 0, join)
      this.modelInfo.joins.splice(0, 0, join)
    },
    removeJoin(row, index) {
      this.dataSource.splice(index, 1)
      this.modelInfo.joins.splice(index, 1)
    }
  }
}
</script>

<style scoped>

</style>
